
const EventEmitter = require('events') 
const peer = new EventEmitter()
const {ipcRenderer, desktopCapturer} = require('electron')

const { systemPreferences } = require('electron')
const { resolve } = require('path')

function getAllWindow(){

    return new Promise((resolve, reject) => {
        let allwindow = desktopCapturer.getSources({ types: ['window', 'screen'] });
        resolve(allwindow);
    })
}

async function getScreenStreamNew(){

    return new Promise((resolve,reject) =>{
        desktopCapturer.getSources({ types: ['window', 'screen'] }).then(async sources => {
            for (const source of sources) {
                // console.log('source='+JSON.stringify(source))
              if (source.name === 'Document') {
                navigator.webkitGetUserMedia({
                    audio: false,
                    video: {
                        mandatory: {
                            chromeMediaSource: 'desktop',
                            chromeMediaSourceId: source.id,
                            maxWidth: window.screen.width,
                            maxHeight: window.screen.height
                        }
                    }
                }, (stream) => {
                    console.log('add-stream', stream)
                    resolve(stream) 
                }, reject)
                }
            }
       })
    })


}


async function getScreenStreamNew2(sourceId){
    console.log('sourceId='+sourceId)
    return new Promise((resolve,reject) =>{
                navigator.webkitGetUserMedia({
                    audio: false,
                    video: {
                        mandatory: {
                            chromeMediaSource: 'desktop',
                            chromeMediaSourceId: sourceId,
                            maxWidth: window.screen.width,
                            maxHeight: window.screen.height
                        }
                    }
                }, (stream) => {
                    console.log('add-stream', stream)
                    resolve(stream) 
                }, reject)
    })
}


async function getScreenStream() {
    const sources = await desktopCapturer.getSources({types: ['window', 'screen']})
    console.log('source='+JSON.stringify(sources))

    console.log('sources[0]'+JSON.stringify(sources[0]))
    return new Promise((resolve, reject) => {
        navigator.webkitGetUserMedia({
            audio: false,
            video: {
                mandatory: {
                    chromeMediaSource: 'desktop',
                    chromeMediaSourceId: sources[0].id,
                    maxWidth: window.screen.width,
                    maxHeight: window.screen.height
                }
            }
        }, (stream) => {
            console.log('add-stream', stream)
            resolve(stream)
            
        }, reject)
    })
}

function selectScreen(sourceId){
    return getScreenStreamNew2(sourceId)
    
}


function startScreen(path){
     
}

function endScreen(){
     
}

module.exports = {getAllWindow,selectScreen,startScreen,endScreen}